import request from '@/utils/request.js'
// 导入
export function importDB(data) {
  return request.post(rewriteURL('/data/ed/database/import'), data)
}
// 导出
export function exportDB(params, type) {
  return request.get(rewriteURL('/data/ed/file/batchExport', type), {
    params,
    responseType: 'blob',
    customMessageError: true,
    timeout: Infinity,
  })
}
// 增
export function createFolder(data, type) {
  return request.post(rewriteURL('/data/ed/file/createFolder', type), data)
}
// 更新文件占用状态
export function updateFileStatus(data) {
  return request.post(rewriteURL('/data/ed/file/updateFileStatus'), data)
}
// 上传
export function uploadFile(data) {
  return request.post(rewriteURL('/data/ed/file/upload'), data)
}
// 下载
export function downloadFile(params, type) {
  return request.get(
    rewriteURL('/data/ed/file/download', type),
    {
      params,
      responseType: 'blob',
      customMessageError: true,
      timeout: Infinity,
    },
    // {
    //   customInterceptorsResponse: true,
    // },
  )
}

// 下载
export function downloadFileStructure(data, type) {
  return request.post(
    '/data/ed/struct/table/download',
    data,
    { responseType: 'blob', customMessageError: true, timeout: Infinity },
    // {
    //   customInterceptorsResponse: true,
    // },
  )
}
// 复制
export function copyFile(data) {
  return request.post(rewriteURL('/data/ed/file/copy', type), data)
}
// 重命名
export function renameFile(data) {
  return request.post(rewriteURL('/data/ed/file/rename', type), data)
}
// 查
export function getFileInfoList(data, type) {
  return request.post(rewriteURL('/data/ed/file/getFileInfoList', type), data)
}
// 更新文件信息
export function updateFileInfo(data, type) {
  return request.post(rewriteURL('/data/ed/file/updateFileInfo', type), data)
}
export function getCategoryTree(params, type) {
  return request.get(rewriteURL('/data/ed/file/tree', type), { params })
}
export function getChildFileCount(data) {
  return request.post(rewriteURL('/data/ed/file/getChildFileCount', type), data)
}
// 合并
export function mergeChunks(params, type) {
  return request.get(rewriteURL('/data/ed/file/mergeChunks', type), {
    params,
    timeout: Infinity,
  })
}
// 移动
export function fileMove(params, type) {
  return request.get(rewriteURL('/data/ed/file/moveFile', type), {
    params,
  })
}
// 复制
export function fileCopy(params, type) {
  return request.get(rewriteURL('/data/ed/file/copyFile', type), {
    params,
  })
}
// 查询列表
export function getFileInfo(data, type) {
  return request.post(rewriteURL('/data/ed/file/info', type), data)
}
//查询单条
export function getFileDetail(params, type) {
  return request.get(rewriteURL('/data/ed/file/fileDetail', type), {
    params,
  })
}
//关系新建
export function createRelation(data, type) {
  return request.post(rewriteURL('/data/ed/relation/create', type), data)
}
//关系取消
export function cancelRelation(params, type) {
  return request.get(rewriteURL(`/data/ed/relation/cancel/${params.id}`, type))
}
//关系描述修改
export function updateRelation(data, type) {
  return request.post(rewriteURL('/data/ed/relation/update', type), data)
}
//关系list
export function listRelations(params, type) {
  return request.get(
    rewriteURL(`/data/ed/relation/listRelations/${params.id}`, type),
  )
}

//文件作废
export function deleteFile(params, type) {
  return request.get(rewriteURL('/data/ed/file/delete', type), {
    params,
  })
}
//获取发布列表
export function uploadRecord(params, type) {
  return request.get(rewriteURL('/data/ed/file/uploadRecord', type), {
    params,
  })
}
//版本关系
export function getVersionView(params, type) {
  return request.get(rewriteURL('/data/ed/file/versionView', type), {
    params,
  })
}
//版本回退
export function versionBack(params, type) {
  return request.get(rewriteURL('/data/ed/file/versionBack', type), { params })
}

export const permissionPersonal = {
  preview: true,
  move: true,
  download: true,
  import: false,
  edit: true,
  upload: true,
  delete: true,
  export: false,
}
//权限查询
export function getPermission(params, type) {
  if (type === 'personal') {
    return { data: permissionPersonal }
  }
  return request.get(`/data/ed/permission/${params.id}`)
}
//检测文件是否存在
export function checkFileNameExist(data, type) {
  return request.post(
    rewriteURL('/data/ed/relation/checkFileNameExist', type),
    data,
  )
}

//本地文件上传并关联
export function relationUpload(data, type) {
  return request.post(rewriteURL(`/data/ed/relation/upload`, type), data, {
    timeout: Infinity,
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  })
}

//查询当前目录下的文件夹
export function queryChildFolder(params, type) {
  return request.get(rewriteURL('/data/ed/file/queryChildFolder', type), {
    params,
  })
}

//输出文件流
export function getFilePreview(params, type) {
  return request.get(rewriteURL('/data/ed/file/preview', type), {
    params,
    responseType: 'blob',
    customMessageError: true,
    timeout: Infinity,
  })
}

/**
 * 文件格式
 */
export function getFileFormatList(params) {
  return request.get('/data/ed/file/format/list', { params })
}

export function fileFormatAdd(params) {
  return request.get('/data/ed/file/format/add', {
    params,
    // customMessageError: true,
  })
}
export function fileFormatDelete(params) {
  return request.get('/data/ed/file/format/delete', {
    params,
  })
}

export function getStructTable(params) {
  return request.get('/data/ed/struct/table/query', { params })
}

export function updateStructTable(data) {
  return request.post('/data/ed/struct/table/update', data)
}
export function removeStructTable(params) {
  return request.get('/data/ed/struct/table/remove', { params })
}
export function addStructTable(data) {
  return request.post('/data/ed/struct/table/add', data)
}

export function queryPrjInfo(params) {
  return request.get('/data/ed/local/prj/queryPrjInfo', { params })
}
export function queryPrjTree(params) {
  return request.get('/data/ed/local/prj/tree', { params })
}
export function queryImportFileInfo(params) {
  return request.get('/data/ed/local/prj/queryImportFileInfo', { params })
}
export function removePrj(params) {
  return request.get('/data/ed/local/prj/removePrj', { params })
}
export function downloadPrj(params) {
  return request.get('/data/ed/local/prj/download', { params })
}

export function rewriteURL(url, type) {
  // console.log(url, type)
  if (type === 'personal') {
    url = url.replace(/^\/data\/ed\//, '/data/ed/user/')
  } else if (type === 'library') {
    url = url.replace(/^\/data\/ed\//, '/data/ed/repo/')
  }
  return url
}

export function flushDataStatus(params) {
  return request.get('/data/ed/file/flushDataStatus', { params })
}
